<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    button {
        position: absolute;
        right: 50%;
        top:200px;
        width: 100px;
        height: 40px;
        color: #fff;
        background: #000000;
    }
</style>
<body>
    <button>加速</button>
</body>
<script>

    var div = document.createElement('div');
    document.body.appendChild(div);
    div.style.width = "100px";
    div.style.height = "100px";
    div.style.background = "red";
    div.style.position = 'absolute';
    div.style.top = "0";
    div.style.left = "0";
    // div.style.bottom = "0";
    // div.style.right = "0";
    var speed = 5;
    var btn = document.getElementsByTagName('button')[0];
    btn.onclick = function(){
        speed++;
    };
    document.onkeydown = function(e){
        console.log(e);
        switch(e.which){
            case 38:
                div.style.top  = parseInt(div.style.top) - speed +'px';
                break;
            case 40:
                div.style.top  = parseInt(div.style.top) + speed +'px';
                break;
        
            case 37:
                div.style.left  = parseInt(div.style.left) - speed +'px';
                break;
        
            case 39:
                div.style.left  = parseInt(div.style.left) + speed +'px';
                break;
            
        }


    }


    // let speed = 1;
    // var aa=setInterval(function(){
    //     speed += 1;
    //     let left = parseInt(div.style.left);
    //     let top = parseInt(div.style.top);
    //     div.style.left = left + speed + 'px';
    //     div.style.top = top + speed + 'px';
    //     console.log(speed);
    //     if(speed == 50){
    //         console.log(this);
    //         clearInterval (aa);
    //     }
        
    // },50)
</script>
</html>